<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form desktop row" auto-complete="on"
      label-position="left">
      <div class="right">
        <!-- <div class="title">{{ defaultTitle }}</div> -->
        <div class="title">欢迎登录</div>
        <div class="titleZ">Welcome to Order center</div>
        <el-form-item prop="username" class="formItem">
          <div class="inputName">账号</div>
          <el-input ref="username" v-model="loginForm.username" placeholder="请输入登录账号" clearable name="username"
            type="text" tabindex="1" auto-complete="off" />
        </el-form-item>
        <el-form-item prop="password">
          <div class="inputName">密码</div>
          <el-input :key="passwordType" ref="password" v-model="loginForm.password" clearable :type="passwordType"
            placeholder="请输入登录密码" name="password" tabindex="2" auto-complete="off" @keyup.enter.native="handleLogin" />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <el-form-item prop="verify" class="formItem">
          <div class="inputName">验证码</div>
          <el-input ref="verify" v-model="loginForm.verify" placeholder="请输入验证码" clearable name="verify" type="text"
            tabindex="1" auto-complete="off" />
          <div class="verifyImg">
            <img src="" alt="">
          </div>
        </el-form-item>
        <div class="remember">
          <div><el-checkbox label="" name="type" />记住账号</div>
          <div>忘记密码?</div>
        </div>
        <el-button :loading="loading" type="primary" class="btn fz20"
          @click.native.prevent="handleLogin">立即登录</el-button>
      </div>
    </el-form>
    <!-- <div class="tip">copyright  guoquan.com  2020</div> -->
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate';
import { randomLenNum } from '@/utils/tools';
import { mapState, mapActions } from 'vuex';
export default {
  name: 'Login',
  data () {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入正确的用户名'));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能少于6位'));
      } else {
        callback();
      }
    };
    const validateVerify = (rule, value, callback) => {
      if (!value) {
        callback(new Error('验证码不能为空'));
      } else if (value.length !== 4) {
        callback(new Error('验证码必须为4位'));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: '18888888881', // 登陆账号
        password: '123456a', // 密码
        verify: ''// 验证码
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ],
        verify: [
          { required: true, trigger: 'blur', validator: validateVerify }
        ]
      },
      loading: false,
      passwordType: 'password',
      codeImg: '',
      redirect: undefined
    };
  },
  computed: {
    ...mapState({
      defaultTitle: state => state.settings.title
    })
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  mounted () {
    this.getCode();
  },
  methods: {
    // 获取菜单
    ...mapActions('permission', [
      'getMenus'
    ]),
    // 切换密码显示隐藏
    showPwd () {
      if (this.passwordType === 'password') {
        this.passwordType = '';
      } else {
        this.passwordType = 'password';
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    // 获取图形验证码
    getCode () {
      this.loginForm.randomStr = randomLenNum(8, true);
      // this.codeImg = window.location.origin + '/code?randomStr=' + this.loginForm.randomStr;
      // this.codeImg = process.env.VUE_APP_BASE_API + '/code?randomStr=' + this.loginForm.randomStr;
      this.codeImg = window.server.SERVER_API[0].api + '/code?randomStr=' + this.loginForm.randomStr;
    },
    // 登录
    handleLogin () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch('permission/login', this.loginForm)
            .then(() => {
              this.getMenus()
                .then(() => {
                  // console.log('跳转地址', process.env.VUE_APP_PUBLICPATH);
                  this.$router.push({ path: this.redirect || '/archives/formula/index' });
                  this.loading = false;
                });
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log('检验失败');
          return false;
        }
      });
    },
    // 切换登陆模式
    changeMode (type) {
      if (type === 'wechat') return this.$message.warning('微信扫码登录暂未开放！');
      this.loginForm.grant_type = type;
    }
  }
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #fff;
$light_gray: #333;
$cursor: #93a0ae;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .desktop {
    .el-input {
      display: inline-block;
      width: 75%;
      font-size: 22px;
      box-sizing: content-box;
      input {
        background: transparent;
        border: 0;
        -webkit-appearance: none;
        border-radius: 0;
        color: $cursor;
        caret-color: $cursor;
        &:-webkit-autofill {
          box-shadow: 0 0 0 1000px $bg inset !important;
          -webkit-text-fill-color: $light_gray !important;
        }
      }
    }
    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: $bg;
      border-radius: 5px;
      color: $cursor;
    }
    .el-form-item__content {
      position: relative;
      border: 1px solid #e7e7e7;
      padding: 0 20px;
      line-height: 60px;
      display: flex;
      .inputName {
        font-size: 18px;
        margin-right: 14px;
      }
      .verifyImg {
        position: absolute;
        right: 0;
        width: 200px;
        line-height: 60px;
        background: #eee;
        cursor: pointer;
      }
      input::-webkit-input-placeholder {
        font-size: 18px;
      }
      input::-moz-input-placeholder {
        font-size: 18px;
      }
      input::-ms-input-placeholder {
        font-size: 18px;
      }
    }
    .remember {
      display: flex;
      justify-content: space-between;
      color: $cursor;
      .el-checkbox {
        margin-right: 6px;
      }
      .el-checkbox__inner {
        // width: 16px;
        // height: 16px;
      }
      > div:nth-child(2) {
        text-decoration: underline;
      }
    }
  }
}
</style>

<style lang="scss" scoped>
$bg: #22467a;
$dark_gray: #889aa4;
$light_gray: #eee;
$zoom: 937 / 1080;

.login-container {
  position: relative;
  min-height: 100%;
  width: 100%;
  // background-color: $bg;
  background: url("../../assets/images/loginBg.png") no-repeat #eee;
  background-size: 100% 100%;
  overflow: hidden;
  // PC端样式
  .login-form.desktop {
    zoom: $zoom;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 1200px;
    height: 700px;
    max-height: 100vh;
    max-width: 100%;
    background: #ffffff;
    background: url("../../assets/images/login-logo.png") no-repeat 14% center /
      450px 449px #fff;
    border-radius: 20px;
    .right {
      zoom: $zoom;
      position: absolute;
      top: 0;
      right: 100px;
      width: 600px;
      height: 100%;
      padding: 100px 60px 120px;
      .title {
        font-size: 40px;
        line-height: 40px;
        font-weight: bold;
      }
      .titleZ {
        font-size: 20px;
        line-height: 60px;
        margin-bottom: 20px;
        color: #999;
      }
      .mode {
        display: flex;
        justify-content: flex-start;
        .mode-item {
          color: #93a0ae;
          font-size: 26px;
          line-height: 46px;
        }
        .mode-item.selected {
          color: #48a0ff;
          border-bottom: 2px solid #48a0ff;
        }
      }
      .show-pwd {
        position: absolute;
        right: 10px;
        top: 50%;
        transform: translateY(-50%);
        font-size: 16px;
        color: $dark_gray;
        cursor: pointer;
        user-select: none;
      }
      .code-img {
        position: absolute;
        right: 10px;
        top: 50%;
        transform: translateY(-50%);
        width: 120px;
        height: 40px;
        cursor: pointer;
      }
      .btn {
        width: 360px;
        height: 70px;
        display: block;
        border-radius: 35px;
        margin: 64px auto 0;
        background: $bg;
        border-color: $bg;
      }
    }
  }
  .tip {
    font-size: 20px;
    color: #fff;
    position: fixed;
    bottom: 30px;
    left: 50%;
    transform: translateX(-50%);
  }
}
</style>
